package com.example.yls.demob;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by yls on 2019/5/15.
 */

public class MyView extends View {
    public static final int PEN = 0 ;
    public static final int RECT = 1 ;
    public static final int CIRCLE = 2 ;

    private Point rectLeftTop = new Point();
    private Point rectRightBottom =new Point();
    private Point cicleCenter =new Point();
    private float radius;
    private Paint paint= new Paint();
    private Path path = new Path();
    private int mode = PEN;

    public MyView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setStyle(Paint.Style.STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mode){
            case PEN:
                canvas.drawPath(path,paint);
                break;
            case RECT:
                canvas.drawRect(rectLeftTop.x,rectLeftTop.y,
                        rectRightBottom.x,rectRightBottom.y,paint);
                break;
            case CIRCLE:
                canvas.drawCircle(cicleCenter.x,cicleCenter.y,radius,paint);
        }

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        Log.e("MyView","x = " +event.getX()+" y = " + event.getY());

        switch (mode){
            case PEN:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        path.moveTo(event.getX(),event.getY());
                        break;

                    case MotionEvent.ACTION_MOVE:
                        path.lineTo(event.getX(),event.getY());
                        break;
                        case MotionEvent.ACTION_UP:
                            break;
                }

            case RECT:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        rectLeftTop.x= (int) event.getX();
                        rectLeftTop.y = (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        rectRightBottom.x= (int) event.getX();
                        rectRightBottom.y= (int) event.getY();
                        break;

                    case MotionEvent.ACTION_UP:
                        break;
                }
            case CIRCLE:
                switch (event.getAction()){
                    case MotionEvent.ACTION_DOWN:
                        cicleCenter.x= (int) event.getX();
                        cicleCenter.y = (int) event.getY();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        radius = (float) Math.sqrt(Math.pow(event.getX()-cicleCenter.x,2)
                                + Math.pow(event.getY()-cicleCenter.y,2));
                        invalidate();
                        break;

                    case MotionEvent.ACTION_UP:
                        break;
                }
        }

        return true;
    }

    public void setPaintColor(int paintColor) {
        paint.setColor(paintColor);
    }
    public void setPaintMode(int mode) {
        this.mode = mode;
    }
}
